/**
  ******************************************************************************
  * @file    SL_FOC.c
  * @author  Silan - MC Team
  * @version 1.0.0
  * @date    2017/07/19
  * @brief   This file is main function.
  * @details None
	* @note    None
  ******************************************************************************
  * @attention
  * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  * AS A RESULT, SILAN MICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
	*
  ******************************************************************************
	* Change History
	* -# 2017/07/19 | v1.0.0 | Wangjianlong | Creat file.
	*
	******************************************************************************
  */

/*******************************************************************************
 * Include file
 ******************************************************************************/
#include "SC32F58128.h"                 // Device header

#include "devInit.h"
#include "BSP_Adc.h"
#include "timer4Tick.h"
#include "eev_ctl.h"

#include "api_fan_pmsm_pfc.h"
#include "api_dbg.h"
#include "interface.h"

/*******************************************************************************
 * Global data for the project
 ******************************************************************************/
uint8_t g_u8ErrorClearFlag __attribute__ ((at(ADDR_ERROR_CLEAR)));

/*******************************************************************************
 * Local data for the current file
 ******************************************************************************/
uint32_t  mg_u32EevStep = 450;
uint32_t  mg_u32IdlePercent0 = 0;
uint32_t  mg_u32IdlePercent1 = 0;
uint32_t  mg_u32IdleCnt0 = 0;
uint32_t  mg_u32IdleCnt1 = 0;

/*******************************************************************************
 * Local prototypes function
 ******************************************************************************/


/*******************************************************************************
 * Local function for the current file
 ******************************************************************************/

/**
  * @brief   Second Core Main Function.
	* @param   None
	* @retval  void
  */
int main_c1(void)
{
	INIT_DeviceInit_c1();

  TICK_TimerInit4C1(&g_Tmr4C1);
	while(1)
	{
    mg_u32IdleCnt1++;   // for code idle test

    TICK_TimerTickRunning(&g_Tmr4C1);

    if(IS_1MS_FLG_SET(g_Tmr4C1))
    {
      CLR_1MS_FLG(g_Tmr4C1);
    }

    if(IS_10MS_FLG_SET(g_Tmr4C1))
    {
      CLR_10MS_FLG(g_Tmr4C1);
    }

    if(IS_100MS_FLG_SET(g_Tmr4C1))
    {
      CLR_100MS_FLG(g_Tmr4C1);
    }
	}
}

/**
  * @brief   Main Function.
	* @param   None
	* @retval  void
  */
int main(void)
{
  uint32_t u32tmp;

  /* Device Init */
  INIT_DeviceInit();

  #ifdef DUAL_CORE
    CHIP_KEY_ENABLE;
    CHIPCTL->CLKEN_H23 = 0x5affffff;      // Core1 clock enable
    CHIP_KEY_ENABLE;
    CHIPCTL->SRST_REQ_H23 = 0xffffffff;   // Reset core 1
    CHIP_KEY_ENABLE;
    CHIPCTL->SRST_REQ_H23 = 0x5affffff;   // Reset release
  #endif

	/* Software Timer Init */
  TICK_TimerInit4C0(&g_Tmr4C0);

  while(1)
	{
    mg_u32IdleCnt0++;   // for code idle test

    /* Software Timer Update */
    TICK_TimerTickRunning(&g_Tmr4C0);

    /* 1mS Task */
    if(IS_1MS_FLG_SET(g_Tmr4C0))
    {
      CLR_1MS_FLG(g_Tmr4C0);

      DBG_RxPackageDeal();

      /* Get temperature ad result for a/c */
      if(ADC->ADCINTFLG_b.ADCINT4)
      {
        ADC->ADCINTFLGCLR_b.ADCINT4 = 1;
        GetSmpData(ADC_SMP_GRP_AC);
      }
    }

    /* 10mS Task */
    if(IS_10MS_FLG_SET(g_Tmr4C0))
    {
      CLR_10MS_FLG(g_Tmr4C0);

      DriverErrorProcess();

      PmsmMainLoopFunction();
      PfcMainLoopFunction();
      #if FAN_CTRL_EN
        FanMainLoopFunction();
      #endif

      mg_EEV_SetStepPoint(mg_u32EevStep);
      mg_EEV_StepServo();
    }

    /* 100mS Task */
    if(IS_100MS_FLG_SET(g_Tmr4C0))
    {
      CLR_100MS_FLG(g_Tmr4C0);

      /* Sample delay and window debug code of fan control */
//      u32tmp = (g_sFanModule.sPmsm.sPmsmPwmGen.sPara.u32SmpDlyClks>>1);
//      ACCESS_ENABLE;
//      ADC->ADCSOC3CTL_b.INDLY = u32tmp;
//      ACCESS_DISABLE;

      if(g_u8ErrorClearFlag){
        g_u8ErrorClearFlag = 0;
        CommParameterWrite(M_CLEARERROR,1);
      }
    }
	}
}

/*---------------------------------- End of the file ---------------------------------*/
